var app = getApp()
var common = require("../../../utils/common")
var http = require("../../../utils/httpUtils")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: ['11', '22', '33', '44', '55', '66'],
    dataInfo: {},
    activityId: 0,
    organizationId: 0,
    organizationDesc: ''
  },

	// 拨打机构电话
	freeTell: function(){
    wx.makePhoneCall({
      phoneNumber: '18591951503',
    })
  },
  // 收藏
  collectBtn: function () {
    var that = this
    var urls = app.globalData.SERVER_IP + '/organization/user_collect'
    var params = {
      "uc_fid": that.data.activityId,
      "uc_uid": wx.getStorageSync('userInfo').userid,
      "uc_type": "0"
    }
    http.P(urls, params, (res) => {
      console.log("获取收藏信息", res.result)
      var str = ''
      if(res.result.reCollect){
        str = res.result.reCollect
        that.setData({
          'dataInfo.isCollectData':1
        })
      }else if(res.result.cancleCollect){
        str = res.result.cancleCollect
        that.setData({
          'dataInfo.isCollectData':0
        })
      }
      wx.showToast({
        title: str,
        icon: 'success',
        duration: 2000
      })
    }, (err) => {
      console.log("获取收藏数据失败")
      console.log(err)
    })

  },
  getData: function () {
    var that = this
    // 获取数据
    var urls = app.globalData.SERVER_IP + '/organization/search_organization_course_detail/o_id/' + that.data.organizationId + '/oc_id/' + that.data.activityId + '/u_id/' + wx.getStorageSync('userInfo').userid
    http.G(urls, {}, (res) => {

      console.log("获取信息", res.result)
      that.setData({
        dataInfo: res.result
      })

      that.setData({
        organizationDesc: common.richContentFormat(common.escape2Html(res.result.organizationCourseData[0].oc_desc))
      })
    }, (err) => {
      console.log("获取信息失败")
      console.log(err)
    })

  },

  courseBtn: function (e) {
    wx.redirectTo({
      url: '../../activity/course/course?activityId=' + e.target.dataset.id + '&o_id=692',
    })
  },

  submitBtn: function () {
    wx.navigateTo({
      url: '../pay/submitOrder/submitOrder?activityId=' +
        this.data.activityId +
        '&title=' + this.data.dataInfo.organizationCourseData[0].oc_title +
        '&title_image=' + this.data.dataInfo.organizationCourseData[0].oc_title_image +
        '&new_price=' + this.data.dataInfo.organizationCourseData[0].oc_new_price +
        '&old_price=' + this.data.dataInfo.organizationCourseData[0].oc_old_price +
        '&course_count=' + this.data.dataInfo.organizationCourseData[0].oc_course_count +
        '&o_activity_type=' + '5' +
        '&type=' + '1' +
        '&o_type=' + '2' +
        '&o_organization_name=' + this.data.dataInfo.organizationData[0].o_name +
        '&o_id=' + this.data.organizationId,
    })

  },
  bindGetUserInfo(e) {
    console.log(111111)
    console.log(e.detail.userInfo)

    var that = this
    wx.getSetting({
      success(res) {
        // 获取用户基础资料和code
        var that = this;
        var userInfo = {};
        wx.login({
          success(loginRes) {
            // 获取code
            console.log("获取code", loginRes)
            userInfo.code = loginRes.code

            // wx.getUserInfo({
            //   success: userInfoRes => {
            //获取用户信息
            // console.log("获取用户信息",userInfoRes)
            // userInfo.nickname = userInfoRes.userInfo.nickName
            // userInfo.headerurl = userInfoRes.userInfo.avatarUrl
            userInfo.nickname = "游客"
            userInfo.headerurl = "https://www.baidu.com/img/flexible/logo/pc/result.png"

            // 获取登录信息
            var urls = app.globalData.WX_SERVER + '/app/wx/microAppLogin'
            http.P(urls, userInfo, (res) => {
              console.log("获取登录信息", res.data)

              // 存储登录信息
              wx.setStorage({
                key: "userInfo",
                data: res.data
              })

              // 查询登录信息
              console.log(wx.getStorageSync('userInfo'))
            }, (err) => {
              console.log("获取信息失败")
              console.log(err)
            })

            //   }
            // })

          }
        })
      }
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.data.activityId = options.activityId
    this.data.organizationId = options.o_id
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
		})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
 
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (options) {
		var that = this;
		// 设置菜单中的转发按钮触发转发事件时的转发内容
		var shareObj = {
			title: this.data.dataInfo.organizationCourseData[0].oc_title, // 默认是小程序的名称(可以写slogan等)
			path: '', // 默认是当前页面，必须是以‘/’开头的完整路径
			imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
			success: function (res) {
				// 转发成功之后的回调
				if (res.errMsg == 'shareAppMessage:ok') {}
			},
			fail: function () {
				// 转发失败之后的回调
				if (res.errMsg == 'shareAppMessage:fail cancel') {
					// 用户取消转发
				} else if (res.errMsg == 'shareAppMessage:fail') {
					// 转发失败，其中 detail message 为详细失败信息
				}
			}
		}
		// 返回shareObj
		return shareObj;
	}
})